package patientimport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.util.CollectionUtils;
import org.testng.annotations.Test;

import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.followup.plat.entity.Patient;
import com.bbcare.followup.plat.entity.PatientContentData;
import com.bbcare.followup.plat.service.IFamilyReportService;
import com.bbcare.followup.plat.store.ibatis.IFamilyReportDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientTaskContentDAO;

@ContextConfiguration(locations = { "classpath*:/ibatis-config.xml",
		"classpath*:/spring-mvc.xml","classpath*:/spring-mybatis.xml"
		})  
public class SynchGrowthQuestionResult extends AbstractTestNGSpringContextTests {

  @Autowired
  IPatientDAO patientdao ;
  
  @Autowired
  IFamilyReportDAO familyReportDAO ;
  
  @Autowired
  IFamilyReportService familyReportService;
  
  @Autowired
  IPatientTaskContentDAO patientTaskContentDAO;
  
  @Test
  public void service()throws Exception {
	  hightService();
	  headService();
	  weightService();
  }
  
  private void weightService() throws Exception {
	  String weightQuestionID= "bd5ea280282";
	  String weightQuestionPer= "bd5eaa50283";
	  
//	  List<Map<String,Object>> list= familyReportDAO.selectAllGrowthRecords(new HashMap<String,Object>());
	  //更新体重 
	  Map<String,Object> paramMap = new HashMap<String,Object>();
	  paramMap.put("questionId",weightQuestionID);
	  List<Map<String,Object>> weightList = patientTaskContentDAO.selectPtContentDataGrowthRecord(paramMap);
	  if(!CollectionUtils.isEmpty(weightList)){
		  Map<String,Object> tempMap = null;
		  for(int i = 0 ;i<weightList.size();i++){
			  try{
				  tempMap = weightList.get(i);
				  Map<String,Object> map = new HashMap<String,Object>();
					map.put("tenantId",0);
					map.put("userId",tempMap.get("user_id"));
					map.put("testTime",tempMap.get("finish_date").toString());
					
					Patient patient = patientdao.selectById(map);
					if(null!= tempMap.get("question_answer")){
						map.put("values",tempMap.get("question_answer"));
						map.put("type",Constants.DEVELOPPROJECT_WEIGHT);
						
						Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,null,patient);
						
						map.clear();
						map.put("questionId",weightQuestionPer);
						map.put("taskId",tempMap.get("task_id"));
						map.put("updateAccept",tempMap.get("update_accept"));
						map.put("questionAnswer",percentiles.get("percentiles"));
						map.put("userId",tempMap.get("user_id"));
						patientTaskContentDAO.updateContentDataGrowthStandard(map);
					}
			  }catch(Exception e){
				  e.printStackTrace();
			  }
		  }
	  }
  }
  
  private void hightService() throws Exception {
	  String HightQuestionID= "bd5eb230284";
	  String HightQuestionPer= "bd5eba00285";
	  
//	  List<Map<String,Object>> list= familyReportDAO.selectAllGrowthRecords(new HashMap<String,Object>());
	  //更新体重 
	  Map<String,Object> paramMap = new HashMap<String,Object>();
	  paramMap.put("questionId",HightQuestionID);
	  List<Map<String,Object>> weightList = patientTaskContentDAO.selectPtContentDataGrowthRecord(paramMap);
	  if(!CollectionUtils.isEmpty(weightList)){
		  Map<String,Object> tempMap = null;
		  for(int i = 0 ;i<weightList.size();i++){
			  try{
			  tempMap = weightList.get(i);
			  Map<String,Object> map = new HashMap<String,Object>();
				map.put("tenantId",0);
				map.put("userId",tempMap.get("user_id"));
				map.put("testTime",tempMap.get("finish_date").toString());
				
				Patient patient = patientdao.selectById(map);
				if(null!= tempMap.get("question_answer")){
					map.put("values",tempMap.get("question_answer"));
					map.put("type",Constants.DEVELOPPROJECT_LENGTH);
					Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,null,patient);
					map.clear();
					map.put("questionId",HightQuestionPer);
					map.put("taskId",tempMap.get("task_id"));
					map.put("updateAccept",tempMap.get("update_accept"));
					map.put("questionAnswer",percentiles.get("percentiles"));
					map.put("userId",tempMap.get("user_id"));
					patientTaskContentDAO.updateContentDataGrowthStandard(map);
				}
			  }catch(Exception e){
				  e.printStackTrace();
			  }
		  }
	  }
  }
  
  private void headService() throws Exception {
	  String HeadQuestionID= "bd5ec1d0286";
	  String HeadQuestionPer= "bd5ec9a0287";
	  
//	  List<Map<String,Object>> list= familyReportDAO.selectAllGrowthRecords(new HashMap<String,Object>());
	  //更新体重 
	  Map<String,Object> paramMap = new HashMap<String,Object>();
	  paramMap.put("questionId",HeadQuestionID);
	  List<Map<String,Object>> weightList = patientTaskContentDAO.selectPtContentDataGrowthRecord(paramMap);
	  if(!CollectionUtils.isEmpty(weightList)){
		  Map<String,Object> tempMap = null;
		  for(int i = 0 ;i<weightList.size();i++){
			  try{
			  tempMap = weightList.get(i);
			  Map<String,Object> map = new HashMap<String,Object>();
				map.put("tenantId",0);
				map.put("userId",tempMap.get("user_id"));
				map.put("testTime",tempMap.get("finish_date").toString());
				
				Patient patient = patientdao.selectById(map);
				if(null!= tempMap.get("question_answer")){
					map.put("values",tempMap.get("question_answer"));
					map.put("type",Constants.DEVELOPPROJECT_HEAD);
					Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,null,patient);
					map.clear();
					map.put("questionId",HeadQuestionPer);
					map.put("taskId",tempMap.get("task_id"));
					map.put("updateAccept",tempMap.get("update_accept"));
					map.put("questionAnswer",percentiles.get("percentiles"));
					map.put("userId",tempMap.get("user_id"));
					patientTaskContentDAO.updateContentDataGrowthStandard(map);
				}
		  }catch(Exception e){
			  e.printStackTrace();
		  }
		  }
	  }
  }
}
  